/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/5/31
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.Controller;

import cn.hutool.extra.ssh.JschUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it1997.it1997admin.pojo.KsData;
import com.it1997.it1997admin.pojo.LinuxServer;
import com.it1997.it1997admin.pojo.Permission;
import com.it1997.it1997admin.pojo.User;
import com.it1997.it1997admin.service.ExtractDataService;
import com.it1997.it1997admin.service.MonitorService;
import com.it1997.it1997admin.utils.JschUtils;
import com.it1997.it1997admin.utils.ResultUtil;
import com.it1997.it1997admin.vo.ExtractVo;
import com.it1997.it1997admin.vo.LinuxServerVo;
import com.it1997.it1997admin.vo.PermissionVo;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Principal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;

@Log4j2
@Controller
@Api(tags = "业务接口")
public class BusinessController {
    @Autowired
    private ExtractDataService extractDataService;
    @Autowired
    private MonitorService monitorService;
    List<Session> sessionList=new ArrayList<>();
    Map map=new HashMap();


    @ApiOperation(value = "抽取数据")
    @PostMapping("/extractData")
    @ResponseBody
    public String extractData(@RequestBody ExtractVo extractVo) {
        log.info(extractVo);
        int i = extractDataService.extractData(extractVo.getMode(), extractVo.getKddm(), extractVo.getExtractKSNumber(), extractVo.getRate());
        if (i == 1) {
            return ResultUtil.success(1, "抽取完成");
        } else {
            return ResultUtil.fail(0, "抽取失败");
        }
    }

    @ApiOperation(value = "获取Linux服务器列表")
    @GetMapping("/getLinuxServer")
    @ResponseBody
    public String getLinuxServer(Integer page, Integer limit) {
        List<LinuxServer> linuxServerList = monitorService.getLinuxServer(page, limit);
        Integer linuxServerCount = monitorService.getLinuxServerCount();
        if (linuxServerList.size() > 0) {
            return ResultUtil.tableData(0, "查询成功", linuxServerCount, linuxServerList);
        } else {
            return ResultUtil.fail(1, "查询失败");
        }
    }
    @ApiOperation(value = "服务器连接ssh")
    @GetMapping("/connServer")
    @ResponseBody
    public String connServer(Integer serverId) {
        log.info("连接中。。。");
        Session connection = JschUtils.createConnection("47.102.104.151", "root", "19971207Xc", 22);
        map.put(serverId,connection);
        log.info(map);
        if(connection!=null)
        return ResultUtil.success(1,"连接成功");
        return ResultUtil.fail(0,"连接失败");
    }
    @ApiOperation(value = "服务器连接ssh")
    @PostMapping("/execCmd")
    @ResponseBody
    public String execCmd(@RequestBody String str) throws IOException {
        log.info(str+"执行中。。。");
        LinuxServerVo linuxServer = JSON.parseObject(str,LinuxServerVo.class);
        log.info(linuxServer);
        //根据服务器id查询服务器用户名密码等信息
        List<LinuxServerVo> linuxServerVoList=new ArrayList<>();
        DecimalFormat df   = new DecimalFormat("######0.00");
        for (Integer serverId:linuxServer.getServerList()){
            log.info("服务器id："+serverId);
            LinuxServerVo linuxServerVo=monitorService.getLinuxServerById(serverId);
            Session session= (Session) map.get(serverId);
            InputStream inputStream = JschUtils.sendCommand(session, "free");
            String  res=JschUtils.returnData(inputStream);
            Map map = JschUtils.splitMemResult(res);
            double memUsed= Double.parseDouble((String) map.get("memUsed"));
            double memTotal= Double.parseDouble((String) map.get("memTotal"));;
            linuxServerVo.setRam(df.format((memUsed/memTotal)*100)+"%");
            linuxServerVoList.add(linuxServerVo);

        }
        return ResultUtil.tableData(0,"查询成功",10,linuxServerVoList);
    }
}
